<?php

/**
 * Created by PhpStorm.
 * User: ls721
 * Date: 2016/10/28 0028
 * Time: 21:16
 */
class Model_Work extends PhalApi_Model_NotORM
{
    public function listWork($doctor_id)
    {

        // $work = $this->getORM();
        // $work->select(
        //     'Work.id as WorkId',
        //     'Event_id as EventId',
        //     'Doctor_id as DoctorId',
        //     'Event.Location as Location',
        //     'Event.Title as Title',
        //     'Work.Status',
        //     'Work.CreateDate'
        // )->where('Doctor_id', $doctor_id);
        // return $work->fetchRows();
        $sql = 'SELECT  E.id AS EventId,E.Lng,E.Lat,W.id as WorkId,W.Doctor_id as DoctorId,W.Status,W.CreateDate,E.Title as Title,E.Location
FROM (
 SELECT Event.id AS id,P.id AS Pid,P.Name As PatientName,Illness_info,Illness,CreateDate,Status,Location_id,Event.Name,PhoneNumber,CallPhone,L.Address AS Address ,L.lng AS Lng,L.lat AS Lat,Event.Title as Title,Event.Location
      FROM Event
        LEFT JOIN Patient As P ON Event.Patient_id=P.id
        LEFT JOIN Location As L ON Event.Location_id=L.id
)As E
LEFT JOIN Work as W ON E.id=W.Event_id
where(W.Doctor_id=:Did)
order by CreateDate desc

';
        $rs = $this->getORM()->queryAll($sql, array(':Did'=>$doctor_id));

        return $rs;

    }

    public function getWorkSingle($work_id)
    {
        return $this->getORM()->select(
            'Work.id as WorkId',
            'Event_id as EventId',
            'Doctor_id as DoctorId',
            'Doctor.Name as DoctorName',
            'Work.Status',
            'Work.CreateDate',
            'Work.ResponseTime'
        )
            ->where('Work.id', $work_id)
          
            ->fetch();

    }

    public function listDoctorOfEvent($event_id)
    {
        return $this->getORM()
            ->select(
                'Work.Doctor_id as DoctorId ',
                'Doctor.Name DoctorName',
                'Doctor,depart',
                'Doctor.PhoneNumber',
                'Work.Status'
            )
            ->where('Event_id', $event_id)

            ->fetchRows();
    }
    public function listDoctorOfWork($event_id)
    {
        return $this->getORM()
            ->select(
                'Work.Doctor_id as DoctorId ',
                'Doctor.Name',
                'Doctor.Depart',
                'Doctor.DepartName',
                'Doctor.Hospital',
                'Doctor.PhoneNumber',
                'Doctor.Lng',
                'Doctor.Lat',
                'Work.Status'
            )
            ->where('Event_id', $event_id)

            ->fetchRows();
    }

    protected function getTableName($id)
    {
        return 'Work';
    }
}